﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL.NewPhotoRindGrid;
using MINERP.COM;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.NewPhotoRindGrid;
using MINERP.DAL;

namespace MINERP.BLL.NewPhotoRindGrid
{
    public class GridBLL_Grid
    {
        /// <summary>
        /// 给某个产品分配格子
        /// </summary>
        /// <param name="orderProductId">要分配的产品ID</param>
        /// <param name="GridId">返回的格子的ID</param>
        /// <param name="IsCompleteD">些产品是否全部分片完成</param>
        /// <returns></returns>
        public int AssignGridByForOrderProductId(int orderProductId, out bool isNewGrid, int curSubAreaId, out Grid_Info gInfo)
        {
            int result = 1;
            isNewGrid = false;
            gInfo = null;

            if (orderProductId > 0)
            {
                GridDAL_Grid _dal = new GridDAL_Grid();
                result = _dal.AssignGridForOrderProductId(orderProductId, (int)SubAreaType.Photo, curSubAreaId, out isNewGrid, out gInfo);
            }
            else
            {
                result = -1;
            }
            return result;
        }

        /// <summary>
        /// 完成时，或是需要清空格子的时候，清空格子
        /// </summary>
        /// <param name="gridId"></param>
        /// <returns></returns>
        public int ClearGrid(int gridId)
        {
            int result = 0;
            if (gridId > 0)
            {
                GridDAL_Grid _dal = new GridDAL_Grid();
                result = _dal.ClearGrid(gridId);
            }
            else
            {
                result = -1;
            }
            return result;
        }

        /// <summary>
        /// 根据格子ID得到格子需要显示的信息信息，包括格子本身的信息，也包括格子中的产品以及照片的信息
        /// </summary>
        /// <param name="gridId"></param>
        /// <returns></returns>
        public GridInfo_Grid GetGridViewInfoByGridId(int gridId,int orderProudctId)
        {
            Grid_Info gridBaseInfo = null;
            GridInfo_Grid gridViewInfo = null;
            OrderInfo_Grid oG = null;
            List<PhotoInfo_Info> photoList;

            OrderInfoBLL_Grid orderBLL = new OrderInfoBLL_Grid();
            PhotoBLL_Grid photoBLL = new PhotoBLL_Grid();
            GridDAL_Grid _dal = new GridDAL_Grid();


            gridBaseInfo = _dal.GetGridViewInfoByGridId(gridId);
            gridViewInfo = new GridInfo_Grid() { GridBaseInfo = gridBaseInfo };
            if (gridBaseInfo.SomeId==0)
            {
                gridBaseInfo.SomeId = orderProudctId;
            } 

            //分别出得相应的OrderInfo_Grid、PhotoInfo_Info
            if (gridBaseInfo.SomeId != 0)
            {
                if (orderBLL.GetOrderInfo_Grid(gridBaseInfo.SomeId, out oG) == 0)
                {
                    int rel = 0;
                    photoList = photoBLL.GetPhotosByOrderProductId(gridBaseInfo.SomeId, out rel);

                    oG.PhotoS = photoList;
                    gridViewInfo.OrderInfo = oG;
                }
            }

            return gridViewInfo;
        }

        /// <summary>
        /// 强制打印格子
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public int ForcePrintGrid(GridInfo_Grid grid, int techId, int opUid, out string strNextTechName, out string shopGridName, out string outProductOrderInBag)
        {
            int result = 1;
            strNextTechName = string.Empty;
            shopGridName = string.Empty;
            outProductOrderInBag = string.Empty;

            if (techId<1||opUid<1||grid.OrderInfo==null||grid.GridBaseInfo==null)
            {
                return -1;
            }
            //先清空格子
           result= ClearGrid(grid.GridBaseInfo.GridId);
           if (result==1)
           {
               //再改流程
               GridDAL_Grid _gridDal = new GridDAL_Grid();
               result = _gridDal.OverTechForForcePrintGrid(opUid, techId, grid.OrderInfo.OrderProductId, out strNextTechName, out shopGridName, out outProductOrderInBag);           
           }

           return result;            
        }


        /// <summary>
        /// 获取合册分区
        /// </summary>
        /// <param name="orderProductIds"></param>
        public List<Grid_Info> GetGridInfoList()
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Grid> gridData = db.Grid.Where(p => p.GridSubArea.Type == 3).OrderBy(p=>p.GridId).ToList();  //只找合册

                foreach (Grid item in gridData)
                {
                    if (!item.GridSubAreaReference.IsLoaded)
                    {
                        item.GridSubAreaReference.Load();
                    }
                    if (!item.GridRecord.IsLoaded)
                    {
                        item.GridRecord.Load();
                    }
                }

                List<orderInfo> orderinfolist= db.orderInfo.ToList();
                List<Tb_OrderBag> orderbaglist= db.Tb_OrderBag.ToList();
                List<Shop> shoplist =db.Shop.ToList();


                List<Grid_Info> list = new List<Grid_Info>();
                foreach (Grid item in gridData)
                {
                    Grid_Info info = new Grid_Info();
                    info.GridId = item.GridId;
                    info.SubAreaId = item.GridSubArea.SubAreaID;
                    info.GridName = item.GridName;
                    info.SomeId = item.SomeId;
                    info.FirstEnterTime = item.FirstEnterTime;
                    info.Status = item.Status;
                    info.GridType = item.GridType;
                    GridRecord_Info inforecord = new GridRecord_Info();
                    if (item.GridRecord != null && item.GridRecord.Count > 0)
                    {
                     
                        inforecord.GridRecordId = item.GridRecord.FirstOrDefault().GridRecordId;
                        inforecord.SomeId = item.GridRecord.FirstOrDefault().SomeId;
                        inforecord.CreateTime = item.GridRecord.FirstOrDefault().CreateTime;
                        if (inforecord.OperateUser == null)
                            inforecord.OperateUser = new User_Info();
                        inforecord.OperateUser.UserID = item.GridRecord.FirstOrDefault().OperateUid;
                        inforecord.Num = item.GridRecord.FirstOrDefault().Num;
                        if (inforecord.SomeId > 0)
                        {
                            orderInfo orinfo = orderinfolist.FirstOrDefault(c => c.OrderProductId == inforecord.SomeId);
                            if (!orinfo.Tb_OrderBagReference.IsLoaded)
                            {
                                orinfo.Tb_OrderBagReference.Load();
                            }
                            Tb_OrderBag tb_odbag = orderbaglist.FirstOrDefault(s => s.OrderBagId == orinfo.Tb_OrderBag.OrderBagId);
                            if (!tb_odbag.ShopReference.IsLoaded)
                            {
                                tb_odbag.ShopReference.Load();
                            }
                            Shop shop = shoplist.FirstOrDefault(ss => ss.ShopId == tb_odbag.Shop.ShopId);

                            OrderInfo_Info order = new OrderInfo_Info();
                            if (shoplist != null)
                            {
                                order.ShopName = shop.shopName;
                            }
                            if (orinfo != null)
                            {
                                order.CtmName = orinfo.CtmName;
                                order.OrderProductName = orinfo.OrderProductName;
                                order.OrderProductCode = orinfo.OrderProductCode;
                            }
                            if (tb_odbag != null)
                            {
                                inforecord.orderinfoModel = order;
                            }
                        }
                    }
                    info.recordModel=inforecord;
                    list.Add(info);
                }
                return list;
            }
        }

        /// <summary>
        /// 清除格子信息,并且只清除合册的格子
        /// </summary>
        /// <param name="gridid"></param>
        /// <returns></returns>
        public bool ClearGridData(int gridid,out string mess)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                mess = "";
                List<Grid> gridData = db.Grid.Where(p => p.GridSubArea.Type == 3).OrderBy(p => p.GridId).ToList();  //只找合册
                Grid gridmodel = gridData.FirstOrDefault(p => p.GridId == gridid);
                if (gridmodel == null)
                {
                    mess = "没有找到此格子";
                    return false;
                }
                //将格子里面的信息清空
                gridmodel.SomeId = 0;
                gridmodel.Status = 0;
                db.SaveChanges();
                //删除记录
                List<GridRecord> recordmodelList = db.GridRecord.ToList();
                foreach (GridRecord rec in recordmodelList)
                {
                    if (!rec.GridReference.IsLoaded)
                    {
                        rec.GridReference.Load();
                    }
                }

                GridRecord mdoel = recordmodelList.FirstOrDefault(p => p.Grid.GridId == gridid);

               // recordmodelList.RemoveAll(p=>p.GridRecordId==mdoel.GridRecordId);

                db.GridRecord.DeleteObject(mdoel);
                //保存
                db.SaveChanges();
                
                return true;
            }
        }
    }
}
